/**
 * 
 */
package io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import gfx.GraphicImage;

/**
 * @author Remi REBILLARD
 *
 */
public class ImportImage {
	private BufferedReader buffer;
	
	private String header;
	private String[] content;
	
	/**
	 * @param inputstream
	 */
	public ImportImage( InputStream is ){
		this.buffer = new BufferedReader( new InputStreamReader( is ) );
	}
	
	/**
	 * @return the data of file for BuildingType
	 */
	public HashMap< String, GraphicImage > read() throws IOException{
		
		HashMap< String, GraphicImage > data = new HashMap< String, GraphicImage >();
		header = buffer.readLine();
		
		if( !header.equals( "game_db_image\t1" ) )
			throw new IOException( "Invalid Header of GraphicImage" );
		
		while( buffer.ready() ){
			
			content = buffer.readLine().split( "\t" );
			GraphicImage tmpImage = new GraphicImage
			(
				content[1],						//PATH
				Integer.parseInt( content[2] ),	//X_OFFSET
				Integer.parseInt( content[3] ),	//Y_OFFSET
				Integer.parseInt( content[4] ),	//NB_ROWS
				Integer.parseInt( content[5] )	//NB_COLUMNS
			);
			data.put( content[0], tmpImage );
		}
		return data;
	}
}
